/**
 * Created by DevilsEyes on 2016/2/13.
 */
'use strict';

var express = require('express');
var router = express.Router();

var $mail = require('../midware/mail');
var Admin_user = require('../midware/admin/user');
var $session = require('../midware/session');

router.get('/login', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    let NotLogged = ()=> {
        res.json({
            code: 0,
            login: false
        });
    };

    if (ssid === undefined) {
        return NotLogged();
    } else {
        let Session = $session.getBySid(ssid);
        if (Session && Session.role >= 9999) {
            Admin_user.searchByMd5Id(Session.md5_userId)
                .then((user)=> {
                    res.json({
                        code: 0,
                        user: Admin_user.sync_filter(user)
                    });
                })
                .catch(()=> {
                    NotLogged();
                })

        } else {
            NotLogged();
        }
    }

});

router.get('/logout', function (req, res, next) {

    res.clearCookie("ssid", {path: '/'});
    res.json({
        code: 0,
        msg: '登出成功!'
    })

});

router.post('/login', function (req, res, next) {

    let data = req.body;
    //sid
    //password
    let _user = null;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {

            if (!data.sid) {
                throw {
                    code: 9999,
                    msg: '没有加密学号'
                };
            }

            if (!data.password) {
                throw {
                    code: 9999,
                    msg: '没有密码'
                };
            }

            return Admin_user.searchByMd5Id(data.sid)
        })
        .then((user)=> {

            if (user == null) {
                throw{
                    code: 9999,
                    msg: '用户不存在'
                };
            }

            if (data.password != user.secPassword) {
                throw{
                    code: 9999,
                    msg: '密码错误'
                };
            }

            if (user.role < 9999) {
                throw{
                    code: 9999,
                    msg: '权限不够'
                };
            }

            _user = user;
            var ssid = $session.setSession(user);
            res.cookie("ssid", ssid);
            res.json({
                code: 0,
                user: Admin_user.sync_filter(_user)
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });

});

router.get('/verify/list', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {

            $session.isAdmin(ssid);
            return Admin_user.getVerifyList();

        })
        .then((users)=> {
            res.json({
                code: 0,
                list: users
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

router.get('/list', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {
            $session.isAdmin(ssid);

            return Admin_user.getUserList();
        })
        .then((users)=> {
            res.json({
                code: 0,
                list: users
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

router.get('/class', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {
            $session.isAdmin(ssid);

            return Admin_user.getAllClass();
        })
        .then((classes)=> {
            res.json({
                code: 0,
                list: classes
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

router.get('/all/list', function (req, res, next) {

    let cookies = req.cookies;
    let ssid = cookies.ssid;

    new Promise((O, X)=> {
        O();
    })
        .then(()=> {
            $session.isAdmin(ssid);

            return Admin_user.getAll();
        })
        .then((users)=> {
            res.json({
                code: 0,
                list: users
            });
        })
        .catch((e)=> {
            res.json({
                code: e.code,
                msg: e.msg
            });
        });
});

router.post('/verify', function (req, res, next) {

    try {

        let cookies = req.cookies;
        let ssid = cookies.ssid;

        let data = JSON.parse(req.body.json);

        let list = data.list;
        let pass = data.pass;

        new Promise((O, X)=> {
            O();
        })
            .then(()=> {
                $session.isAdmin(ssid);

                if (!list || !list.length || list.length == 0) {
                    throw {
                        code: 9999,
                        msg: '没有学号数组'
                    };
                }
                if (!pass) {
                    throw {
                        code: 9999,
                        msg: '不明白是否通过审核'
                    };
                } else {
                    pass = !!pass;
                }

                return Promise.all(list.map((id)=> {

                    if (pass) {
                        return Admin_user
                            .verify(id, pass)
                            .then((user)=> {
                                return $mail.sendVerifySuccessMail(user)
                            });
                    } else {
                        return Admin_user
                            .verify(id, pass)
                    }

                }));
            })
            .then(()=> {
                res.json({
                    code: 0
                });
            })
            .catch((e)=> {
                res.json({
                    code: e.code,
                    msg: e.msg
                });
            });

    } catch (e) {
        res.json({
            code: 9999,
            msg: '接口发生错误'
        });
    }

});

module.exports = router;